<?php
namespace Admin\Controller;
use Think\Controller;
use Think\Page;

/**
* 超级管理员权限管理控制器
*/
class AuthController extends Controller
{
	
	function __construct()
	{
		parent::__construct();

		// if(session('admin') == null)
		// 	$this->error('登录超时，请重新登录', '/Home/Index/adminLogin');
	}


	public function index()
	{
		$auth = M('auth_rule');

		$count = $auth->where(array('status'=>1))->count();
		$page = new Page($count, 8);
        $show = $page->show();
		
        $list = $auth->where(array('status'=>1))->limit($page->firstRow.','.$page->listRows)->field('id, name, title, condition')->select();
		$this->assign('list', $list);
        $this->assign('page', $show);
		$this->display();

	}

	public function edit()
	{
		$id   = isset($_GET['id']) ? $_GET['id'] : 0;
		$auth = M('auth_rule');
        $authItem = $auth->where(array('id'=>$id))->find();

        if(IS_POST)
        {
            if( !$auth->create($_POST, 3) )
			{
                $this->error($auth->getError());
			}	
			else
			{
				$pk = $auth->getPK();
               
				$method = empty($auth->$pk) ? 'add' : 'save';

				if( $auth->$method() )
				{
					$this->success('保存成功');
				}
				else
				{
					$this->error('保存失败');
				}
			}
        }

        $this->assign('authItem', $authItem);

        $this->display();
		
	}

	public function delete()
	{
		if( isset($_GET['id']) )
		{
			if( M('auth_rule')->where(array('id'=>$_GET['id']))->delete() )
			{
				$this->success('删除成功');
			}
			else
			{
				$this->error('删除失败');
			}
		}
	}

	public function group()
	{
		$group = M('auth_group');

		$count = $group->where( array('status'=>1) )->count();

		$page = new Page($count, 8);
        $show = $page->show();

		$list = $group->where( array('status' =>1) )->limit($page->firstRow.','.$page->listRows)->select();
        
        $this->assign('list', $list);
        $this->assign('show', $show);

        $this->display();
	}

	public function group_edit()
	{
		$id    = isset($_GET['id']) ? $_GET['id'] : 0;
        $rule  = M('auth_rule');     
		$group = M('auth_group');
        $groupItem = $group->where(array('id'=>$id) )->find();
   
        $list  = $rule->where(array('status'=>1))->select();
        
        if( IS_POST )
        {
            if( !$group->create($_POST, 3) )
            {
                $this->error($group->getError());
            }
            else
            {
            	$rules = implode(",", $_POST['rules']); 
                $pk = $group->getPK();
                
                $method = empty($group->$pk) ? 'add' : 'save';
                $_POST['rules']  =  $rules;
                 
                if( !$group->$method($_POST) )
                {
                	$this->success('保存失败');
                }
                else
                {
                	$this->error('保存成功');
                }
            }
        }

        $this->assign('groupItem', $groupItem);

        $this->assign('list', $list);
        $this->display();

	}

    // public function 
	public function group_delete()
	{
		$group = M('auth_group');

		if($group->where(array('id'=>$_GET['id']))->delete() )
		{
			$this->success('删除成功');
		}
		else
		{
			$this->error('删除失败');
		}

	}




}
?>